/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Base;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author Higor
 */
public class ArquivoDespesas {
    
    public static boolean salvarDespesas(ArrayList<Object> _despesas){
        
        String file_name = "plan_desp_data.xml";
        FileOutputStream m_fos = null;
        ObjectOutputStream m_out = null;
        XMLEncoder m_xenc;

        try{
            m_fos = new FileOutputStream(file_name);
        }catch(IOException e){
            return false;
        }

        m_xenc = new XMLEncoder(m_fos);

        // Write object.
        m_xenc.writeObject(_despesas);

	m_xenc.flush();
	m_xenc.close();
	try{
            m_fos.close();
        }catch(IOException e){
            return false;
        }
        
        return true;
    }
    
    public static boolean lerDespesas(ArrayList<Object> _despesas){
        String file_name = "plan_desp_data.xml";
        FileInputStream m_fis = null;
        ObjectInputStream m_in = null;
        XMLDecoder m_xdec;

        try{
            m_fis = new FileInputStream(file_name);
        }catch(IOException e){
            return false;
        }

        m_xdec = new XMLDecoder(m_fis);

        ArrayList<Object> list;

        list = new ArrayList<Object>();
        
        try {
            list = (ArrayList<Object>) m_xdec.readObject();
        } catch (Exception e){
            return false;
        }

        if (list == null)
            list = new ArrayList<Object>();
        
        _despesas = list;
        
        return true;         
    }
    
}
    
